package com.microstrategy.web;

import com.microstrategy.web.objects.WebIServerSession;
import com.microstrategy.web.objects.WebObjectsException;
import com.microstrategy.web.objects.WebObjectsFactory;
import com.microstrategy.webapi.DSSXMLClassFactory;

/** This is class for storing user Credential  */

public class Credential
{
	private String m_userName;
	private String m_password;
	private String m_projectName;
	private String m_serverName;
	private int m_port;
	private WebIServerSession m_MstrSession;
	
	static {
		// We want to be able to create connections to a specific IServer
		// without first having to establish a connection pool.  This is largely
		// a feature of the "demo" and has very little to do with External Security,
		// per se.
		DSSXMLClassFactory.getAdminInstance().setAutoConnect(true);		
	}
	
	
	
	public boolean hasMSTRSession() {
        boolean __result = false;
        try {
            // If we don't even have a WebIServerSession object, then return...
            if (m_MstrSession == null) {
                return (__result = false);
            }

            // Refresh...
            m_MstrSession.refresh();

            // Is it alive?
            return (__result = m_MstrSession.isAlive());
        }
        catch (WebObjectsException woe) {
            // Erase the session...
            eraseMSTRSession();

            return (__result = false);
        }
    }

	
	public  WebIServerSession getMSTRSession()
	{
		if(hasMSTRSession())
			return m_MstrSession;
		else
			return null;
	}
	
	public void setMSTRSession(WebIServerSession session)
	{
		m_MstrSession = session;	
	} 
	
	public Credential(String userName, String pwd, String prjName, String srvName, int port)
	{
		
		m_userName = userName;
		m_password = pwd;
		m_projectName = prjName;
		m_serverName = srvName;
		m_port = port;
		try{
		eraseMSTRSession();//byb mod 20120413
		m_MstrSession = WebObjectsFactory.getInstance().getIServerSession();
		m_MstrSession.setLogin(m_userName);
        	m_MstrSession.setPassword(m_password);
        	m_MstrSession.setProjectName(m_projectName);
        	m_MstrSession.setServerName(m_serverName);
        	m_MstrSession.setServerPort(m_port);
        	System.out.println("the sessionId is "+ m_MstrSession.getSessionID());
               } catch(WebObjectsException e)
               {
               	System.out.println("create user error" + e.getMessage());
               	e.printStackTrace();
               	}
        // Remember the password so that we can return it later...
    	}
	
	public void setUserName( String userName)
	{
		m_userName=userName;
	}
	public String getUserName()
	{
		return m_userName;
	}

        public void setPassword(String pwd)
        {
        	m_password = pwd;
        }

	public String getPassword()
	{
		return m_password;
	}
	
	public void setProjectName( String prjName)
	{
		m_projectName = prjName;
	}
	
	public String  getProjectName()
	{
		return m_projectName;
	}
	
	public void setServerName(String srvName)
	{
		m_serverName = srvName;
	}
	public String getServerName()
	{
		return m_serverName;
	}

	public void setPort( int port)
	{
		m_port = port;
	}		

	public int getPort()
	{
		return m_port;
	}

 	private void eraseMSTRSession() {
        try {
            // Do we have a WebIServerSession object?
            if (m_MstrSession != null) {
            	System.out.println("eraseMSTRSession=" + m_MstrSession.getSessionID());
                m_MstrSession.closeSession();
                m_MstrSession = null;
            }
        }
        catch (WebObjectsException woe) {
            System.err.println(woe.getMessage());
        }
    	}

}